package com.terrynoya.fight.animations.movieClip
{
	import com.terrynoya.coreLib.MHashMap;
	import com.terrynoya.coreLib.loader.MSWFLoader;
	import com.terrynoya.fight.animations.Animation;
	import com.terrynoya.fight.animations.AnimationElement;
	import com.terrynoya.fight.animations.AnimationId;
	import com.terrynoya.fight.animations.Clsn;
	import com.terrynoya.fight.animations.IAnimationElement;
	import com.terrynoya.fight.emuns.ClsnType;
	import com.terrynoya.fight.stateMachine.StateNumber;
	import com.terrynoya.fight.util.JSonRect;
	
	import flash.display.MovieClip;

	public class MCAnimationAssets
	{
		private var _loader:MSWFLoader;
		
		private var _animations:MHashMap;
		
		private var _movieClips:MHashMap;
		
		public function MCAnimationAssets(loader:MSWFLoader)
		{
			this._loader = loader;
			this.init();
		}
		
		public function get animations():MHashMap
		{
			return _animations;
		}

		private function init():void
		{
			this._animations = new MHashMap();
			this._movieClips = new MHashMap();
			this.createMcAni(AnimationId.Standing,"stand");
			this.createMcAni(AnimationId.WalkingForward,"walk");
			this.createMcAni(AnimationId.JumpStartFrame,"jumpStartFrame");
			this.createMcAni(AnimationId.StartingJumpingUp,"jumpUp",5);
			this.createMcAni(AnimationId.JumpLand,"jumpLand");
			this.createMcAni(5000,"standHitLight");
			this.createMcAni(5005,"standRecoverLight");
			this.createMcAni(230,"standLightKick");
			this.createMcAni(210,"standHeavyPunch");
			this.createMcAni(240,"standHeavyKick");
			this.createMcAni(10,"standToCrunch");
			this.createMcAni(12,"crunchToStand");
			this.createMcAni(11,"crunching");
			this.createMcAni(21,"walkBack");
			this.createMcAni(600,"jumpLightPunch");
			
			this.createMcAni(5000,"standHitHard");
			this.createMcAni(5001,"standHitHard");
			this.createMcAni(5002,"standHitHard");
			this.createMcAni(5010,"standHitLow");
			this.createMcAni(5011,"standHitLow");
			this.createMcAni(5012,"standHitLow");
			
			this.createMcAni(5047,"standHitRecover");
			this.createMcAni(5048,"standHitRecover");
			this.createMcAni(5049,"standHitRecover");
			this.createMcAni(5050,"standHitRecover");
			this.createMcAni(5051,"standHitRecover");
			
			this.recoverStopAtLast();
			this.createStandLightPunch();
		}
		
		private function recoverStopAtLast():void
		{
			var ani:Animation = this.getAni(5005);
			var ele:IAnimationElement = ani.getElement(ani.elementLength - 1);
			ele.ticks = -1; 
		}
		
		private function createMcAni(id:int,name:String,loopStart:int = 0):MCAnimation
		{
			var mc:MovieClip = this._loader.getDisplayByName(name) as MovieClip;
			var ani:MCAnimation = new MCAnimation(id,mc,loopStart);
			this._animations.put(id,ani);
			this._movieClips.put(id,mc);
			return ani;
		}
		
		private function createStandLightPunch():void
		{
			var rects:Array = [
				"{X:28 Y:-102 Width:44 Height:21}",
				"{X:-6 Y:-104 Width:32 Height:53}",
				"{X:-24 Y:-49 Width:61 Height:51}"
			];
			var ani:MCAnimation = this.createMcAni(200,"standLightPunch");
			this.setElementClsns(ani,3,rects);
			this.setElementClsns(ani,4,rects);
		}
		
		private function setElementClsns(ani:MCAnimation,eleIndex:int,clsns:Array):void
		{
			var ele:IAnimationElement = ani.getElement(eleIndex);
			ele.clsns = this.parseClsn(clsns);
		}
		
		private function parseClsn(infos:Array):Array
		{
			if(infos == null)
			{
				return null;
			}
			var arr:Array =  new Array();
			for (var i:int = 0; i < infos.length; i++) 
			{
				var cln:Clsn = new Clsn(ClsnType.Type1Attack);
				var str:String = infos[i];
				cln.rect = JSonRect.parse(str);
				arr.push(cln);
			}
			return arr;
		}
		
		public function getAni(stateId:int):MCAnimation
		{
			return this._animations.getValue(stateId);
		}
		
		public function getMovieClip(id:int):MovieClip
		{
			return this._movieClips.getValue(id);
		}
		
		public function hasAnimation(id:int):Boolean
		{
			return this._animations.getValue(id);
		}
	}
}